package org.smardi.fakeCall.Setting;

import org.smardi.fakeCall.*;

import android.app.*;
import android.content.*;
import android.database.*;
import android.media.*;
import android.net.*;
import android.os.*;
import android.provider.*;
import android.view.*;
import android.widget.*;

public class AC_Setting extends Activity {

	//뒤로가기
	TextView btn_back;
	
	//적용하기
	Button btn_apply;
	
	//전화상대 이름 Edittext
	EditText edit_caller_name;
	//전화상대 번호 
	EditText edit_caller_number;
	//전화 번호 임의 생성
	Button btn_caller_number;
	
	//주소록에서 선택하기
	Button btn_get_address;
	
	//벨소리 선택하기
	Button btn_ringtone;
	
	//진동 켜기
	CheckBox cbox_vibrator;
	//벨소리 사용하기
	CheckBox cbox_ringtone;
	
	//SharedPreference
	SharedPreference_FakeCall mPref = null;
	
	Uri ringtoneURI = null;
	String callerName = null;
	String callerNumber = null;
	
	private final int REQUESTCODE_RINGTONE = 0;
	private final int REQUESTCODE_CONTACT = 1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ac_setting);
		
		
		initComponent();
		
		loadPreference();
	}


	/**
	 * View들을 초기화한다
	 */
	private void initComponent() {
		btn_back = (TextView)findViewById(R.id.btn_top_left);
		
		btn_apply = (Button)findViewById(R.id.btn_apply);
		
		edit_caller_name = (EditText)findViewById(R.id.edit_caller_name);
		edit_caller_number = (EditText)findViewById(R.id.edit_caller_number);
		btn_caller_number = (Button)findViewById(R.id.btn_caller_number);
		
		btn_get_address = (Button)findViewById(R.id.btn_get_address);
		btn_ringtone = (Button)findViewById(R.id.btn_ringtone);
		
		cbox_vibrator = (CheckBox)findViewById(R.id.cbox_vibrator);
		cbox_ringtone = (CheckBox)findViewById(R.id.cbox_ringtone);
		
		//SharedPreference
		mPref = new SharedPreference_FakeCall(AC_Setting.this);
		
		registEvent();
	}
	
	private void registEvent() {
		btn_back.setOnClickListener(viewClickListener);
		btn_apply.setOnClickListener(viewClickListener);
		
		btn_caller_number.setOnClickListener(viewClickListener);
		
		btn_get_address.setOnClickListener(viewClickListener);
		btn_ringtone.setOnClickListener(viewClickListener);
	}
	
	private void loadPreference() {
		callerName = mPref.getCallerName();
		edit_caller_name.setText(callerName);
		
		callerNumber = mPref.getCallerNumber();
		edit_caller_number.setText(callerNumber);
		
		cbox_vibrator.setChecked(mPref.isVibratorUsable());
		cbox_ringtone.setChecked(mPref.isRingtoneUsable());
		
		ringtoneURI = mPref.getRingtoneURI();
	}
	
	
	
	View.OnClickListener viewClickListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			switch(v.getId()) {
			case R.id.btn_top_left:
				finish();
				break;
			case R.id.btn_apply:
				apply();
				finish();
				break;
				//--------------------------------------------------
			case R.id.btn_caller_number:
				edit_caller_number.setText(makeRandomPhoneNumber());
				break;
				//--------------------------------------------------
			case R.id.btn_get_address:
				Intent contactIntent = new Intent(Intent.ACTION_PICK);
				contactIntent.setType(ContactsContract.Contacts.CONTENT_TYPE);
				startActivityForResult(contactIntent, REQUESTCODE_CONTACT);
				break;
			case R.id.btn_ringtone:
				Intent ringtoneIntent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
				startActivityForResult(ringtoneIntent, REQUESTCODE_RINGTONE);
				break;
			}
		}

		private String makeRandomPhoneNumber() {
			
			String number = "010";
			for (int i = 0; i < 8; i++) {
				number += Math.round(Math.random()*9);
			}
			return number;
		}
	};
	
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		
		if(resultCode != Activity.RESULT_OK) {
			return;
		}
		
		switch(requestCode) {
		case REQUESTCODE_RINGTONE:
			ringtoneURI = data.getParcelableExtra(RingtoneManager.EXTRA_RINGTONE_PICKED_URI);
			break;
		case REQUESTCODE_CONTACT:
			Uri dataUri = data.getData();
			contactPick(dataUri);
			break;
		}
	}

	


	/**
	 * 주소록에서 선택한 상대의 이름과 번호를 가져온다
	 * @param dataUri
	 */
	private void contactPick(Uri dataUri) {
		Cursor cursor = managedQuery(dataUri, null, null, null, null);
		
		while(cursor.moveToNext()) {
			int getcolumnId = cursor.getColumnIndex(ContactsContract.Contacts._ID);
			
			String id = cursor.getString(getcolumnId);
			callerName = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
			
			String hasPhoneNumber = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
			
			if(hasPhoneNumber.equalsIgnoreCase("1")) {
				hasPhoneNumber = "true";
			} else {
				hasPhoneNumber = "false";
			}
			
			if(Boolean.parseBoolean(hasPhoneNumber)) {
				Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+id, null, null);
				
				while (phones.moveToNext()) {
					callerNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
				}
				
				phones.close();
			}
		}
		
		edit_caller_name.setText(callerName);
		edit_caller_number.setText(callerNumber); 
	}


	//------------------------------------설정 저장
	private void apply() {
		mPref.setCallerName(edit_caller_name.getText().toString());
		mPref.setCallerNumber(edit_caller_number.getText().toString());
		mPref.setRingtoneURI(ringtoneURI);
		mPref.setRingtoneUsable(cbox_ringtone.isChecked());
		mPref.setVibratorUsable(cbox_vibrator.isChecked());
	}
}
